<?php
$this->breadcrumbs()->addPage('Tipos de Processo', 'tiposprocesso');
$this->breadcrumbs()->addPage('Editar Tipo de Processo', 'tiposprocesso', 'editar', array('id' => $this->id));
$this->breadcrumbs()->addPage('Assuntos', 'tiposprocesso', 'assuntos', array('id' => $this->id));
$this->breadcrumbs()->addPage('Editar Assunto', 'assuntos', 'editar', array('id' => $this->id));
$this->breadcrumbs()->addPage((($this->isEdit) ? 'Editar' : 'Inserir') . ' Formulário');
echo $this->breadcrumbs()->render();

if ($this->isEdit) {
    echo $this->tabs()->openList();
    echo $this->tabs()->addTab($this->simpleUrl('editar', null, array('id' => $this->id)), 'Dados Gerais');
    echo $this->tabs()->addTab($this->simpleUrl('formulario', null, array('id' => $this->id)), 'Formulários', true);
    echo $this->tabs()->closeList();
}

echo $this->pagenav()->openList();
echo $this->pagenav()->defaultHelperLinks('formulario.html');
echo $this->pagenav()->closeList();
?>

<!--<div id="article">
        <form id="target" action="post">
                <div id="xsdform_container"></div>
                <input type="submit" />
                <input name="processed_xml" type="hidden" />
        </form>
    <?php //$this->xsdForms($this->id, "processed_xml", "target") ?>
</div>-->

<div id="article">
  <form method="post">
    <dl>
      <dt>Tipo de Processo:</dt>
      <dd><?php echo $this->assunto->tipoProcesso->nome ?></dd>
      <dt>Assunto:</dt>
      <dd><?php echo $this->assunto->nome ?></dd>
    </dl>
    <dl>
      <dt>Nome do Formulário:</dt>
      <dd><input type="text" name="xsdcreator_name" /></dd>
    </dl>
    <div id="xsdformfield_group"></div>
    <dl>
      <dt><a href="#" id="xsdformfield_add">Adicionar item</a></dt>
      <dd>&nbsp;</dd>
    </dl>
    <input type="submit" value="Enviar" />
  </form>
</div>

<div id="xsdformfield_model">
  <dl class="xsdformfield" style="background-color:#E5E4DF;padding-top:1em;"> <!-- pôr TODO estilo no css -->
    <dt>Nome</dt>
    <dd><input type="text" name="xsdcreator_label[]" /> <input type="checkbox" name="xsdcreator_required[]" />Campo Obrigatório</dd>
    <dt>Tipo</dt>
    <dd>
      <select name="xsdcreator_type[]">
        <option value="string">Texto</option>
        <option value="textarea">Área de Texto (texto grande)</option>
        <option value="integer">Número (somente número)</option>
        <option value="date">Data</option>
        <option value="select">Seletor (múltiplas opções)</option>
      </select>
    </dd>
    <dt>&nbsp;</dt>
    <dd><a href="#" class="xsdformfield_del">Remover este item</a></dd>
  </dl>

  <dl class="xsdformfield_select">
    <dt>&nbsp;</dt>
    <dd><a href="#" class="xsdformfield_select_add">Adicionar opção</a></dd>
  </dl>

  <dl>
    <dt class="xsdformfield_select_option">Valor</dt>
    <dd>
      <input type="text" name="xsdcreator_select_options[][]" />
      <a href="#" class="xsdformfield_select_del">Remover esta opção</a>
    </dd>
  </dl>
</div>

<?php
$js = <<<VAR
$(document).ready(function() {
    var xsdformfield_select_counter = 0;
    $("#xsdformfield_add").click(function() {
        $("#xsdformfield_group").append($("#xsdformfield_model dl.xsdformfield").clone());

        $("#xsdformfield_group input[name='xsdcreator_label[]']").attr('name',
                                                                       'xsdcreator_label[' + xsdformfield_select_counter  + ']');
        $("#xsdformfield_group select[name='xsdcreator_type[]']").attr('name',
                                                                      'xsdcreator_type[' + xsdformfield_select_counter  + ']');
        $("#xsdformfield_group input[name='xsdcreator_required[]']").attr('name',
                                                                          'xsdcreator_required[' + xsdformfield_select_counter  + ']');
        xsdformfield_select_counter++;

        $("#xsdformfield_group a.xsdformfield_del:last").click(function() {
            $(this).parent().parent().remove();
            return false;
        });

        $("#xsdformfield_group select:last").change(function() {
            if ($(this).find("option:selected").val() == 'select') {
                $(this).parent().append($("#xsdformfield_model dl.xsdformfield_select").clone());

                $(this).parent().find("a.xsdformfield_select_add").click(function() {
                    $(this).parent().parent().append(
                        $("#xsdformfield_model dt.xsdformfield_select_option").clone(),
                        $("#xsdformfield_model dt.xsdformfield_select_option + dd").clone()
                    );
                    
                    var counter = $(this).parent().parent().parent()
                        .find("select[name^='xsdcreator_type']").attr('name').match(/\[(\d+)\]/)[1];

                    $(this).parent().parent()
                        .find("input[name='xsdcreator_select_options[][]']")
                        .attr('name',
                              "xsdcreator_select_options[" + counter + "][]");

                    $(this).parent().parent().find("a.xsdformfield_select_del:last").click(function() {
                        $(this).parent().prev().remove();
                        $(this).parent().remove();
                        return false;
                    });

                    return false;
                });
            } else {
                $(this).parent().find("dl.xsdformfield_select").remove();
            }
        });

        return false;
    });
});
VAR;

$this->headScript()->appendScript($js, 'text/javascript');
